import os

from lxml import etree

class MainApp:
    def __init__(self):
        self.newproject = False
        self.projectname=""
        self.projectlocation=""
    
    def run(self):
        input = raw_input("Please choose one of the following:\n" +
                                "1. Start a new project\n" +
                                "2. Open an existing project\n" +
                                "Choice: ")
        while(input not in ['1','2']):
            input = raw_input("Please choose either 1 (new project) or " +
                                        "2 (existing project)!!: ")
        if(input == '1'):
            self.newproject = True
            self.create_project()
 
        elif(input == '2'):
            self.read_project()
            
    def create_project(self):
        configroot = etree.Element('ProjectConfig')
        configtree = etree.ElementTree(configroot)
        
        projectname = etree.SubElement(configroot, 'ProjectName')
        proname = raw_input("Enter Project Name: ")
        projectname.text = proname

        projecthome = etree.SubElement(configroot, 'ProjectHome')
        prohome = raw_input("Enter directory location for the " +
                                        "new project: ")
        while(not os.path.isdir(prohome)):
            prohome = raw_input("Please enter a VALID directory!!: ")
        prohome = prohome + os.path.sep + proname
        if(not os.path.isdir(prohome)):
            os.makedirs(prohome)
        projecthome.text = prohome

        dbconfig = etree.SubElement(configroot, 'DBConfig')
        dbhost = raw_input("Enter database host location for the " +
                                        "new project: ")
        dbconfig.set('dbhost', dbhost)
        dbusername = raw_input("Enter database username: ")
        dbconfig.set('dbusername', dbusername)
        dbpassword = raw_input("Enter database password: ")
        dbconfig.set('dbpassword', dbpassword)
        
        modelconfig = etree.SubElement(configroot, 'ModelConfig')
        
        longterm = etree.SubElement(modelconfig, 'LongTermModels')
        workstat = etree.SubElement(longterm, 'WorkStat')
        workstat.set('type', 'Multinomial Logit')
        workstat.set('altspec', 'True')
        alts = etree.SubElement(workstat, 'Alternatives')
        worker = etree.SubElement(alts, 'Worker')
        self.attach_vars(worker)
        nonworker = etree.SubElement(alts, 'NonWorker')
        var1 = etree.SubElement(nonworker, 'Variable')
        var1.set('db', 'somedb')
        var1.set('table', 'sometab')
        var1.set('var', 'constant')
        var1.set('coeff', '0.0')
        numjobs = etree.SubElement(longterm, 'NumJobs')
        numjobs.set('type', 'Negative Binomial')
        alts = etree.SubElement(numjobs, 'Alternatives')
        alts.append(etree.Element('Alternative', id='0'))
        alts.append(etree.Element('Alternative', id='1'))
        alts.append(etree.Element('Alternative', id='2'))
        vars = etree.SubElement(numjobs, 'Variables')
        self.attach_vars(vars)
        schstat = etree.SubElement(longterm, 'SchStat')
        schstat.set('type', 'Probability Distribution')
        cats = etree.SubElement(schstat, 'Categories')
        cat1 = etree.Element('Category', id='0-5yrs')
        cats.append(cat1)
        dist = etree.SubElement(cat1, 'Distribution')
        dist.set('yes','0.9')
        dist.set('no','0.1')
        cat2 = etree.Element('Category', id='5-14yrs')
        cats.append(cat2)
        dist = etree.SubElement(cat2, 'Distribution')
        dist.set('yes','0.6')
        dist.set('no','0.4')
        resloc = etree.SubElement(longterm, 'ResLoc')
        resloc.set('type', 'Multinomial Logit')
        resloc.set('altspec', 'False')
        vars = etree.SubElement(resloc, 'Variables')
        self.attach_vars(vars)


        fixedact = etree.SubElement(modelconfig, 'FixedActivityModels')
        workloc = etree.SubElement(fixedact, 'WorkLoc')
        workloc.set('type', 'Multinomial Logit')
        workloc.set('altspec', 'False')
        vars = etree.SubElement(workloc, 'Variables')
        self.attach_vars(vars)
        schloc = etree.SubElement(fixedact, 'SchoolLoc')
        schloc.set('type', 'Multinomial Logit')
        schloc.set('altspec', 'False')
        vars = etree.SubElement(schloc, 'Variables')
        self.attach_vars(vars)
        preschloc = etree.SubElement(fixedact, 'PreSchoolLoc')
        preschloc.set('type', 'Multinomial Logit')
        preschloc.set('altspec', 'False')
        vars = etree.SubElement(preschloc, 'Variables')
        self.attach_vars(vars)

        vehown = etree.SubElement(modelconfig, 'VehicleOwnershipModels')
        numveh = etree.SubElement(vehown, 'NumVehs')
        numveh.set('type', 'Ordered Probit')
        alts = etree.SubElement(numveh, 'Alternatives')
        alts.append(etree.Element('Alternative', id='0'))
        alts.append(etree.Element('Alternative', id='1'))
        alts.append(etree.Element('Alternative', id='2'))
        vars = etree.SubElement(numveh, 'Variables')
        self.attach_vars(vars)
        thresh = etree.SubElement(numveh, 'Thresholds')
        thresh.append(etree.Element('Threshold', values='1.5'))
        thresh.append(etree.Element('Threshold', values='2.5'))
        vehtype = etree.SubElement(vehown, 'VehTypes')
        vehtype.set('type', 'Nested Logit')
        vehtype.set('altspec', 'True')
        nests = etree.SubElement(vehown, 'Nests')
        nest1 = etree.SubElement(nests, 'Nest')
        nest1.set('id', 'Car')
        nest1.append(etree.Element('Alternative', id='Car-Gas'))
        nest1.append(etree.Element('Alternative', id='Car-Nongas'))
        nest2 = etree.SubElement(nests, 'Nest')
        nest2.set('id', 'SUV')
        nest2.append(etree.Element('Alternative', id='SUV-Gas'))
        nest2.append(etree.Element('Alternative', id='SUV-Nongas'))
        alts = etree.SubElement(vehown, 'Alternatives')
        alt1 = etree.SubElement(alts , 'Car-Gas')
        self.attach_vars(alt1)
        alt2 = etree.SubElement(alts , 'Car-Nongas')
        self.attach_vars(alt2)    
        
        prism = etree.SubElement(modelconfig, 'FixedActivityPrismModels')
        daystart = etree.SubElement(prism, 'DayStart')
        daystart.set('type', 'Stochastic Frontier')        
        self.attach_vars(daystart)
        dayend = etree.SubElement(prism, 'DayEnd')
        dayend.set('type', 'Stochastic Frontier')   
        self.attach_vars(dayend)
        numwrk = etree.SubElement(prism, 'NumWorkEpisodes')
        numwrk.set('type', 'Negative Binomial')
        alts = etree.SubElement(numwrk, 'Alternatives')
        alts.append(etree.Element('Alternative', id='0'))
        alts.append(etree.Element('Alternative', id='1'))
        alts.append(etree.Element('Alternative', id='2'))
        vars = etree.SubElement(numwrk, 'Variables')
        self.attach_vars(vars)
        wrkstrt1 = etree.SubElement(prism, 'WorkStart1')
        wrkstrt1.set('type', 'Stochastic Frontier')   
        self.attach_vars(wrkstrt1)
        wrkend1 = etree.SubElement(prism, 'WorkEnd1')
        wrkend1.set('type', 'Stochastic Frontier')
        self.attach_vars(wrkend1)
        numsch = etree.SubElement(prism, 'NumSchEpisodes')
        numsch.set('type', 'Negative Binomial')
        alts = etree.SubElement(numsch, 'Alternatives')
        alts.append(etree.Element('Alternative', id='0'))
        alts.append(etree.Element('Alternative', id='1'))
        alts.append(etree.Element('Alternative', id='2'))
        vars = etree.SubElement(numsch, 'Variables')
        self.attach_vars(vars)
        schstrt1 = etree.SubElement(prism, 'SchStart1')
        schstrt1.set('type', 'Stochastic Frontier')   
        self.attach_vars(schstrt1)
        schend1 = etree.SubElement(prism, 'SchEnd1')
        schend1.set('type', 'Stochastic Frontier')
        self.attach_vars(schend1)
        preschstrt = etree.SubElement(prism, 'PreSchStart')
        preschstrt.set('type', 'Stochastic Frontier')   
        self.attach_vars(preschstrt)
        preschend = etree.SubElement(prism, 'PreSchEnd')
        preschend.set('type', 'Stochastic Frontier')
        self.attach_vars(preschend)

        child = etree.SubElement(modelconfig, 'ChildStatusAllocationModels')
        schdailystat = etree.SubElement(child, 'SchDailyStatus')
        schdailystat.set('type', 'Probability Distribution')
        cats = etree.SubElement(schdailystat, 'Categories')
        cat1 = etree.Element('Category', id='0-4yrs')
        cats.append(cat1)
        dist = etree.SubElement(cat1, 'Distribution')
        dist.set('yes','0.9')
        dist.set('no','0.1')
        cat2 = etree.Element('Category', id='5-14yrs')
        cats.append(cat2)
        dist = etree.SubElement(cat2, 'Distribution')
        dist.set('yes','0.6')
        dist.set('no','0.4')
        schdailyindep = etree.SubElement(child, 'SchDailyIndependence')
        schdailyindep.set('type', 'Probability Distribution')
        cats = etree.SubElement(schdailyindep, 'Categories')
        cat1 = etree.Element('Category', id='0-4yrs')
        cats.append(cat1)
        dist = etree.SubElement(cat1, 'Distribution')
        dist.set('yes','0.9')
        dist.set('no','0.1')
        cat2 = etree.Element('Category', id='5-14yrs')
        cats.append(cat2)
        dist = etree.SubElement(cat2, 'Distribution')
        dist.set('yes','0.6')
        dist.set('no','0.4')
        afterschdailyindep = etree.SubElement(child, 'AfterSchDailyIndependence')
        afterschdailyindep.set('type', 'Probability Distribution')
        cats = etree.SubElement(afterschdailyindep, 'Categories')
        cat1 = etree.Element('Category', id='0-4yrs')
        cats.append(cat1)
        dist = etree.SubElement(cat1, 'Distribution')
        dist.set('yes','0.9')
        dist.set('no','0.1')
        cat2 = etree.Element('Category', id='5-14yrs')
        cats.append(cat2)
        dist = etree.SubElement(cat2, 'Distribution')
        dist.set('yes','0.6')
        dist.set('no','0.4')
        aftschacttype = etree.SubElement(child, 'AftSchActivityType')
        aftschacttype.set('type', 'Multinomial Logit')
        aftschacttype.set('altspec', 'True')
        alts = etree.SubElement(aftschacttype, 'Alternatives')
        rec = etree.SubElement(alts, 'Recreation')
        self.attach_vars(rec)
        rethome = etree.SubElement(alts, 'ReturnHome')
        self.attach_vars(rethome)
        aftschactdest = etree.SubElement(child, 'AftSchActDestination')
        aftschactdest.set('type', 'Multinomial Logit')
        aftschactdest.set('altspec', 'False')
        vars = etree.SubElement(aftschactdest, 'Variables')
        self.attach_vars(vars)
        aftschactdur = etree.SubElement(child, 'AftSchActDuration')
        aftschactdur.set('type', 'Logistic Regression')
        vars = etree.SubElement(aftschactdur, 'Variables')
        self.attach_vars(vars)
        
        adultdaily = etree.SubElement(modelconfig, 'AdultDailyStatusModels')
        wrkdailystat = etree.SubElement(adultdaily, 'WorkDailyStatus')
        wrkdailystat.set('type', 'Probability Distribution')
        cats = etree.SubElement(wrkdailystat, 'Categories')
        cat1 = etree.Element('Category', id='All')
        cats.append(cat1)
        dist = etree.SubElement(cat1, 'Distribution')
        dist.set('yes','0.9')
        dist.set('no','0.1')
        wrkhmstrt1 = etree.SubElement(adultdaily, 'WorkAtHomeStart1')
        wrkhmstrt1.set('type', 'Stochastic Frontier')   
        self.attach_vars(wrkhmstrt1)
        wrkhmend1 = etree.SubElement(prism, 'WorkAtHomeEnd1')
        wrkhmend1.set('type', 'Stochastic Frontier')
        self.attach_vars(wrkhmend1)
        
        actsim = etree.SubElement(modelconfig, 'ActTravelSimulator')
        aftschactmode = etree.SubElement(actsim, 'AftSchActivityMode')
        aftschactmode.set('type', 'Multinomial Logit')
        aftschactmode.set('altspec', 'True')
        alts = etree.SubElement(aftschactmode, 'Alternatives')
        sov = etree.SubElement(alts, 'HOV')
        self.attach_vars(sov)
        hov = etree.SubElement(alts, 'Bus')
        self.attach_vars(hov)
        discacttype = etree.SubElement(actsim, 'ActivityType')
        discacttype.set('type', 'Multinomial Logit')
        discacttype.set('altspec', 'True')
        alts = etree.SubElement(discacttype, 'Alternatives')
        rec = etree.SubElement(alts, 'Recreation')
        self.attach_vars(rec)
        rethome = etree.SubElement(alts, 'ReturnHome')
        self.attach_vars(rethome)
        destmode = etree.SubElement(actsim, 'ActDestinationMode')
        destmode.set('type', 'Nested Logit')
        destmode.set('altspec', 'False')
        nests = etree.SubElement(destmode, 'Nests')
        nest1 = etree.SubElement(nests, 'Nest')
        nest1.set('id', 'Destination')
        vars = etree.SubElement(nest1, 'Variables')
        self.attach_vars(vars)        
        nest2 = etree.SubElement(nests, 'Nest')
        nest2.set('id', 'Mode')
        alts = etree.SubElement(nest2, 'Alternatives')
        alt1 = etree.SubElement(alts , 'SOV')
        self.attach_vars(alt1)
        alt2 = etree.SubElement(alts , 'HOV')
        self.attach_vars(alt2)           
        discactdur = etree.SubElement(actsim, 'ActivityDuration')
        discactdur.set('type', 'Logistic Regression')
        vars = etree.SubElement(discactdur, 'Variables')
        self.attach_vars(vars)
        fixedactmode = etree.SubElement(actsim, 'FixedActivityMode')
        fixedactmode.set('type', 'Multinomial Logit')
        fixedactmode.set('altspec', 'True')
        alts = etree.SubElement(fixedactmode, 'Alternatives')
        sov = etree.SubElement(alts, 'HOV')
        self.attach_vars(sov)
        hov = etree.SubElement(alts, 'Bus')
        self.attach_vars(hov)
        jointact = etree.SubElement(actsim, 'JointActivity')
        jointact.set('type', 'Probability Distribution')
        cats = etree.SubElement(jointact, 'Categories')
        cat1 = etree.Element('Category', id='All')
        cats.append(cat1)
        dist = etree.SubElement(cat1, 'Distribution')
        dist.set('yes','0.5')
        dist.set('no','0.5')
        tripveh = etree.SubElement(actsim, 'TripVehicle')
        tripveh.set('type', 'Multinomial Logit')
        tripveh.set('altspec', 'False')
        vars = etree.SubElement(tripveh, 'Variables')
        self.attach_vars(vars)


        configfileloc = prohome + os.path.sep + "config.xml"
        if(os.path.isfile(configfileloc)):
            os.remove(configfileloc)
        configfile = open(configfileloc, 'w')
        configtree.write(configfile, pretty_print=True)
        configfile.close()
    
    def attach_vars(self, node):
        var1 = etree.SubElement(node, 'Variable')
        var1.set('db', 'somedb')
        var1.set('table', 'sometab')
        var1.set('var', 'somevar')
        var1.set('coeff', '0.5')
        var2 = etree.SubElement(node, 'Variable')
        var2.set('db', 'somedb')
        var2.set('table', 'sometab')
        var2.set('var', 'somevar')
        var2.set('coeff', '0.6')          
    
    def read_project(self):
        prohome = raw_input("Enter the directory for existing project:")
        while(not os.path.isdir(prohome)):
            prohome = raw_input("Please enter a VALID directory for existing " +
                                            "project!!: ")
        configfileloc = prohome + os.path.sep + "config.xml"
        configtree = etree.parse(configfileloc)
        print configtree


def main():
    app = MainApp()
    app.run()

if __name__ == "__main__":
    main()